/*
 * ThreadHeaderList.java
 *
 * Created on 2005/01/21 +0900
 *
 * $Revision: 58 $
 * $Date: 2008-12-06 08:59:45 +0000 (Sat, 06 Dec 2008) $
 */

package org.gikolet.bbs.data;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.gikolet.framework.util.Arrays;
import org.gikolet.framework.util.Assert;
import org.gikolet.framework.util.Listener;
import org.gikolet.framework.util.ListenerList;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 58 $ $Date: 2008-12-06 08:59:45 +0000 (Sat, 06 Dec 2008) $
 */
public class ThreadHeaderList {
    private Hashtable threadHeaders;

    private ListenerList listeners;

    public ThreadHeaderList() {
	this.threadHeaders = new Hashtable();
	this.listeners = new ListenerList();
    }

    public void set(ThreadHeader threadHeader, int index) {
	Assert.isNotNull(threadHeader);

	this.threadHeaders.put(new Integer(index), threadHeader);

	fireThreadHeaderListChanged(threadHeader, index, index);
    }

    protected void fireThreadHeaderListChanged(ThreadHeader threadHeader,
	    int startIndex, int toIndex) {
	Assert.isNotNull(threadHeader);

	Listener[] listeners = this.listeners.getListeners();
	for (int i = 0; i < listeners.length; i++) {
	    ((ThreadHeaderListChangedListener) listeners[i])
		    .threadHeaderListChanged(threadHeader, startIndex, toIndex);
	}
    }

    public synchronized int indexOf(int threadNo) {
	return indexOf(threadNo, 0);
    }

    public synchronized int indexOf(int threadNo, int index) {
	Enumeration e = this.threadHeaders.elements();
	int i = 0;
	while (e.hasMoreElements()) {
	    ThreadHeader th = (ThreadHeader) e.nextElement();
	    if (index <= i) {
		// if (th.getThreadNo() == threadNo) {
		// return i;
		// }
	    }
	    i++;
	}
	return -1;
    }

    public synchronized ThreadHeader getThreadHeader(int index) {
	return (ThreadHeader) this.threadHeaders.get(new Integer(index));
    }

    public synchronized int getStartIndex(int index) {
	if (this.threadHeaders.containsKey(new Integer(index))) {
	    int i = index - 1;
	    while (true) {
		if (!this.threadHeaders.containsKey(new Integer(i))) {
		    return i + 1;
		}
		i--;
	    }
	}
	return -1;
    }

    public synchronized int getEndIndex(int index) {
	if (this.threadHeaders.containsKey(new Integer(index))) {
	    int i = index + 1;
	    while (true) {
		if (!this.threadHeaders.containsKey(new Integer(i))) {
		    return i - 1;
		}
		i++;
	    }
	}
	return -1;
    }

    public synchronized boolean isEmpty() {
	return this.threadHeaders.isEmpty();
    }

    public synchronized int[] getKeys() {
	int[] wi = new int[this.threadHeaders.size()];
	Enumeration e = this.threadHeaders.keys();

	for (int i = 0; i < wi.length; i++) {
	    Integer oi = (Integer) e.nextElement();
	    wi[i] = oi.intValue();
	}

	Arrays.sort(wi);

	return wi;
    }

    public synchronized void removeAt(int index) {
	this.threadHeaders.remove(new Integer(index));
    }

    public synchronized boolean containsIndex(int index) {
	return this.threadHeaders.containsKey(new Integer(index));
    }

    public synchronized boolean contains(ThreadHeader header) {
	return this.threadHeaders.contains(header);
    }

    public synchronized void clear() {
	this.threadHeaders.clear();
    }
}
